package vno.sci.window
{

	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import org.aswing.AsWingManager;
	import org.aswing.AssetBackground;
	import org.aswing.BorderLayout;
	import org.aswing.JFrame;
	import org.aswing.JLabel;
	import org.aswing.JPopup;
	import org.aswing.JScrollPane;
	import org.aswing.JTextArea;
	import org.aswing.JTextField;
	import org.aswing.JViewport;
	import org.aswing.event.FrameEvent;
	
	import vno.fux.res.pool.BitmapAttacher;
	import vno.sci.event.MonologEvent;
	
	public class ScrollMessageBox extends Sprite
	{
		private var _title:JLabel;
		private var _textArea:JTextArea;
		private var _scroll:JScrollPane;
		private var _popup:JPopup;
		private var _properWidth:int;
		private var _properHeight:int;
		
		/**
		 * 
		 * @param title 
		 * @param text
		 * @param _width 宽度，0为自动调节
		 * @param _height 高度，0为自动调节
		 * @param vsbPolicy 垂直滚动条策略，默认为需要时显示
		 * @param hsbPolicy 水平滚动条策略，默认为需要时显示
		 * @param frameBackground 大背景
		 * @param textBackground 文字背景
		 * @example
		 * ScrollMessageBox("title", "text", width=300, height=200, vsbPolicy = JScrollPane.SCROLLBAR_ALWAYS);//定宽定高，有滚动条
		 * ScrollMessageBox("title", "text", width=300, height=0, vsbPolicy = JScrollPane.SCROLLBAR_AS_NEED);//定宽,高度自动调节，所以无滚动条
		 */		
		public function ScrollMessageBox(title_:String = "",
										 text_:String = "",
										 width_:int = 0,
										 height_:int = 0,
										 vsbPolicy_:int = 0,
										 hsbPolicy_:int = 0,
										 frameBackground_:BitmapAttacher = null,
										 textBackground_:BitmapAttacher = null)
		{
			//initialize AsWing
			AsWingManager.initAsStandard(this);
			//create a JFrame
			_popup = new JPopup(this);
			_popup.setLayout(new BorderLayout());
			_textArea = new JTextArea(text_);
			_textArea.setWordWrap(true);
			_textArea.setEditable(false);
			
			_properWidth = width_;
			_properHeight = height_;
			
			_title = new JLabel(title_);
			
			_scroll = new JScrollPane(_textArea,vsbPolicy_,hsbPolicy_);
			
			if(title_ != "")
			{
				_popup.append(_title, BorderLayout.NORTH);
			}
			_popup.append(_scroll,BorderLayout.CENTER);
			_popup.setWidth(_properWidth?_properWidth:_textArea.getPreferredWidth());
			_popup.setHeight(_properHeight?_properHeight:_textArea.getPreferredHeight());
			_popup.show();

			// add skin to frame and text.
			if (frameBackground_ && textBackground_)
			{
				var frameDecorator:AssetBackground = new AssetBackground(frameBackground_);
				_popup.setBackgroundDecorator(frameDecorator);
				var textDecorator:AssetBackground = new AssetBackground(textBackground_);
				_textArea.setBackgroundDecorator(textDecorator);
			}
			
			// 延迟一会调整宽度与高度
			var timer:Timer = new Timer(1000);
			var bar:Function = function (event:TimerEvent):void 
			{
				timer.removeEventListener(TimerEvent.TIMER, bar);
				timer = null;
				adjust();
			}
			timer.addEventListener(TimerEvent.TIMER, bar);
			timer.start();
		}
		
		private function adjust():void
		{
			var w:int =_properWidth?_properWidth:_textArea.getPreferredWidth();
			var h:int = _properHeight?_properHeight:_textArea.getPreferredHeight();
			_popup.setWidth(w);
			_popup.setHeight(h+_title.height);
			_scroll.setWidth(w);	
			_scroll.setHeight(h);
		}
		public function setText(s:String):void
		{
			_textArea.setHtmlText(s);
			adjust();
		}
		public function close():void
		{
			if(this.parent!=null)
			{
				try
				{
					parent.removeChild(this);
				}
				catch(e:Error)
				{
					
				}
			}
		}
	}
}